//
//  JANewFeaureView.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/5.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit

class JANewFeaureView: UIView {

    /// scrollView
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView(frame: self.bounds)
        //隐藏滚动条
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        //开启分页效果
        scrollView.isPagingEnabled = true
        //关闭弹簧效果
        scrollView.bounces = false
        scrollView.delegate = self
        
        return scrollView
        
    }()
    
     /// pageControl
    lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.pageIndicatorTintColor = UIColor.black
        pageControl.currentPageIndicatorTintColor = globalColor
        pageControl.currentPage = 0
        pageControl.numberOfPages = 4
        
        return pageControl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: screenBounds)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    


}

extension JANewFeaureView {
 
    func setupUI () {
        //添加子视图
        addSubview(scrollView)
        addSubview(pageControl)
        
        //自动布局
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-100)
        }
        
      //添加新特性图片
        for i in 1...4 {
            let imageView = UIImageView(imageName: "new_feature_\(i)")
            imageView.frame = screenBounds.offsetBy(dx: CGFloat((i-1)) * screenWidh, dy: 0)
            scrollView.addSubview(imageView)
            
        }
        
        scrollView.contentSize = CGSize(width: screenWidh*5, height: screenHeight)
    }
}

extension JANewFeaureView: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offSetx = scrollView.contentOffset.x
        
        //计算当前页数
        let page = Int(offSetx/screenWidh + 0.5)//四舍五入算法
        pageControl.currentPage = page
        
        //隐藏pageControl
        pageControl.isHidden = offSetx > (screenWidh * 3.3)
        
          //将newFeature从页面移除
        if(offSetx >= screenWidh * 4) {
            self.removeFromSuperview()
        }
        
    }
}
